package com.pacy.pacybaseproject.constviews;

import android.content.Context;
import android.content.res.TypedArray;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.core.content.ContextCompat;

import com.pacy.pacybaseproject.R;

import io.reactivex.annotations.Nullable;
import me.goldze.mvvmhabit.utils.KLog;


/**
 * @author PACY
 * @date 2022/10/20
 * description:
 */
public class CyclingSelectView extends LinearLayout {

    private ImageView image_iv;

    int selectIcon;
    int unSelectIcon;

    boolean isSelect = true;

    public CyclingSelectView(Context context) {
        super(context);
    }

    public CyclingSelectView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);

        /**
         * 加载布局  获取自定义属性
         */
        LayoutInflater.from(context).inflate(R.layout.cycling_select_item_view, this, true);
        TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.CyclingSelectView);
        selectIcon = a.getResourceId(R.styleable.CyclingSelectView_cycSelectIcon, R.drawable.danche_select_true);
        unSelectIcon = a.getResourceId(R.styleable.CyclingSelectView_cycUnSelectIcon, R.drawable.danche_select_false);
        a.recycle();
    }

    @Override
    protected void onFinishInflate() {
        super.onFinishInflate();
        image_iv = findViewById(R.id.image_iv);
        setSelectType();
    }

    /**
     * 设置图标
     *
     * @param iconResource
     */
    public void setIcon(int iconResource) {
        image_iv.setImageResource(iconResource);
    }

    /**
     * 设置标题
     */
    public void setBgColor(int color) {
        image_iv.setBackgroundColor(color);
    }

    public void setSelectType() {
        isSelect = !isSelect;
        if (isSelect) {
            setIcon(selectIcon);
            image_iv.setBackgroundColor(ContextCompat.getColor(getContext(), R.color.colorPrimary));
        } else {
            setIcon(unSelectIcon);
            image_iv.setBackgroundColor(ContextCompat.getColor(getContext(), R.color.white100));
        }
    }

    public void setSelectType( boolean select) {
        isSelect = select;
        if (isSelect) {
            setIcon(selectIcon);
            image_iv.setBackgroundColor(ContextCompat.getColor(getContext(), R.color.colorPrimary));
        } else {
            setIcon(unSelectIcon);
            image_iv.setBackgroundColor(ContextCompat.getColor(getContext(), R.color.white100));
        }
    }

    public boolean getSelectType() {
        return isSelect;
    }
}
